/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.izenman.Graphisme;

import android.view.GestureDetector;
import android.view.MotionEvent;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import se.izenman.outilsServer.CallRPC;
import se.izenman.outilsServer.EActions;
import se.izenman.outilsServer.ITCPPADFunctions;

/**
 *
 * @author romainmior
 */
public class OurGestureListener extends GestureDetector.SimpleOnGestureListener implements ITCPPADFunctions{
            
    public static Timer TimerMove;
    
    @Override
    public void onLongPress(MotionEvent e){
       
        if(MenuAndroid.RemoteEnCoursName == MenuAndroid.REMOTE_TRACKPAD){
            rightClick();
        }
    }
        
    @Override
    public boolean onSingleTapUp(MotionEvent e){
        if(MenuAndroid.RemoteEnCoursName == MenuAndroid.REMOTE_TRACKPAD){
            leftClick();    
        }
        return true;
    }
    
    @Override
    public boolean onDoubleTap(MotionEvent e){
        if(MenuAndroid.RemoteEnCoursName == MenuAndroid.REMOTE_TRACKPAD){
            doubleClick();
        }
        return true;
    }
    
    
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
        
        if(MenuAndroid.RemoteEnCoursName == MenuAndroid.REMOTE_TRACKPAD){

            //Coordonnées initiales du mouvement
            float xClickDebut = e1.getX();
            float yClickDebut = e1.getY();
            
            //Coordonnées finales du mouvement
            float xClickFin = e2.getX();
            float yClickFin = e2.getY();

            //Calcul le déplacement du doigt à partir des coordonnées
            float xDeplacement = CalculDeplacement(xClickDebut, xClickFin);
            float yDeplacement = CalculDeplacement(yClickDebut, yClickFin);

            //Appel de la fonction move
            move((int)xDeplacement, (int)yDeplacement);
            
        }
        
        return true;
    }
    
    private float CalculDeplacement(float coordDebut, float coordFin) {
        return coordFin - coordDebut;
    }

    public int leftClick() {
        try {
            CallRPC.ExecuteVLC(EActions.LEFTCLICK, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(OurGestureListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int rightClick() {
        try {
            CallRPC.ExecuteVLC(EActions.RIGHTCLICK, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(OurGestureListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int doubleClick() {
        try {
            CallRPC.ExecuteVLC(EActions.DOUBLECLICK, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(OurGestureListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int move(int deplacementX, int deplacementY) {
        
        CallRPC.ExecutePADMove(EActions.MOVE, deplacementX, deplacementY);

        return 0;
    }
}
